import Vue from 'vue'
import axios from 'axios';
import router from '../router';
import TokenCache from "@/utils/cache/TokenCache";
import { Base64 } from "js-base64";
import md5 from "md5";
import QS from "qs";




const service = axios.create({
    baseURL: 'area-api',
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);
// 在发送请求之前做某件事
service.interceptors.request.use(
    (config) => {
      // console.log(config)
      //传给后台请求头
      let time = Math.round(new Date().getTime() / 1000);
      
      let sign = md5(
        // 正式
        // `appid=1712654613&appsecret=41SF3QerJK1TR6FI89VD&timestamp=${time}`
  
        // 测试
        `time=${time}&appname=JYG&appsecret=aD95MDmvd56MDO54qow43QAu9Wd2Sg06`
      );
      //token加密算法
      let token = {
        time: time,
        sign: sign,
        token: TokenCache.getToken(),
      };
      config.headers.token = Base64.encode(JSON.stringify(token));
      
      console.log("time===>"+config.headers.token)
      console.log("config.headers.token===>"+config.headers.token)
      console.log("token===>"+TokenCache.getToken())
      console.log("`time=${time}&appname=JYG&appsecret=aD95MDmvd56MDO54qow43QAu9Wd2Sg06`===========>"+ `time=${time}&appname=JYG&appsecret=aD95MDmvd56MDO54qow43QAu9Wd2Sg06`)
      console.log("sign=====>"+sign)
      config.headers.appname = "JYG";
      config.headers.device = "PC";
      config.headers.time = time;
      console.log(config)
  
      return config;
    },
    (error) => {
      return Promise.reject(error);
    }
);
//返回状态判断(添加响应拦截器)
service.interceptors.response.use(
    (res) => {
      // console.log(res);
      //授权失败
      if (res.data.code == 401) {
        // this.$message.error("登录失效");
        TokenCache.deleteToken();
        location.href = "/";
      }else if (res.data.code != 200) {
        console.log(res.data.msg);
        alert(res.data.msg)
        
      }else {
        return res.data;
      }
    
    },
    (error) => {
      console.log(error);
      let errorMsg = "";
      if (error.message.includes("timeout")) {
        errorMsg = "请求超时!";
      } else {
        errorMsg = "请求异常!";
      }
      return Promise.resolve({ msg: errorMsg });
    }
);

// 请求封装
// get请求方法
export function get(url, params) {
    return new Promise((resolve, reject) => {
      service
        .get(url, {
          params: params,
        })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
}
//   post请求方法
export function post(url, params) {
    return new Promise((resolve, reject) => {
        service
        .post(url, QS.stringify(params))
        .then((res) => {
            resolve(res);
        })
        .catch((err) => {
            reject(err);
        });
    });
}
//  更新方法
export function put(url, params) {
    return new Promise((resolve, reject) => {
        service
        .put(url, QS.stringify(params))
        .then((res) => {
            resolve(res);
        })
        .catch((err) => {
            reject(err);
        });
    });
}
//   批量更新
export function patch(url, params) {
    return new Promise((resolve, reject) => {
        service
        .patch(url, QS.stringify(params))
        .then((res) => {
            resolve(res);
        })
        .catch((err) => {
            reject(err);
        });
    });
}
//   删除方法
export function del(url, params) {
    return new Promise((resolve, reject) => {
        service
        .delete(url, {
            params: params,
        })
        .then((res) => {
            resolve(res);
        })
        .catch((err) => {
            reject(err);
        });
    });
}
  

export default service;
